﻿
helper_area =
{
    parent_id_ = 0;
    game_id_ = 0;
    game_main_type_ = 0;
    cur_game_state_ = nil;

    inner_log = function (msg, core_msg)
        base_utility.log(string.format("[helper_area] %s", msg), core_msg);
    end;

    init = function()
    end;

    init_area = function(parent_id, game_id, game_main_type)
        if game_id == helper_area.game_id_ then
            return;
        end

        helper_area.parent_id_ = parent_id;
        helper_area.game_id_ = game_id;
        helper_area.game_main_type_ = game_main_type;

        local parent_ptr = ui_utility.get_ctrl_ptr_by_id(helper_area.parent_id_);
        if nil == parent_ptr then
            return;
        end

       local panel_id = ui_utility.get_ctrl_id_by_path(helper_area.parent_id_, "HelperAreaCtn");

        -- 设置背景色
        local module_info = banner_model.get_banner_module_info_by_game_id(helper_area.game_id_);
        local bottom_color = banner_model.def_module_color;
        if module_info ~= nil and module_info.bottom_background_color ~= nil and
            module_info.bottom_background_color ~= "" then
            bottom_color = module_info.bottom_background_color;
        end
        --ui_utility.setproperty_by_id(panel_id, 'Background', bottom_color);
        
        -- 切换至高清大区
        helper_area.refresh_attach_game_btn(helper_area.game_id_);
        
        -- 辅助设置
        helper_area.refresh_assist_setting_btn(helper_area.game_id_);

        helper_area.set_download_extension_btn_state();
        base_utility.log("[helper_area]init_area, game_id="..game_id);
        comm_center.listen_broadcast("MsgTGPConfigMgr_UpdateClientMode",
                                     "helper_area.on_color_mode_update");
        
        local color_data_config = comm_center.call_service("SvrTGPConfigMgr_GetColorMode", {});
        if color_data_config ~= nil and color_data_config.color_mode ~=nil then
            local color_data_param = {color_mode = color_data_config.color_mode };
            helper_area.on_color_mode_update(color_data_param);
        end;
    end;

    -- 切换至高清版 按钮状态
    refresh_attach_game_btn = function(game_id)
        local panel_id = ui_utility.get_ctrl_id_by_path(helper_area.parent_id_, "HelperAreaCtn");
        if panel_id == nil or panel_id == Tenio.INVALID_CONTROLID then
            return;
        end

        local entry_id = ui_utility.get_ctrl_id_by_path(panel_id, "CtnMixBtns\\BtnAttachGameEntry");
        if entry_id == nil or entry_id == Tenio.INVALID_CONTROLID then
            return;
        end

        if game_id == nil or tonumber(game_id) == nil or tonumber(game_id) == 0 then
            ui_utility.setproperty_by_id(entry_id, "Visible", "false");
            return;
        end

        local attach_info = comm_center.call_service("Svr_CheckHasAttachRel", 
                {game_id = tonumber(game_id), has_attach = 0, select_id = 0, main_game_id = 0});
        if attach_info == nil or attach_info.has_attach ~= 1 or attach_info.main_game_id == 0 then
            ui_utility.setproperty_by_id(entry_id, "Visible", "false");
            return;
        end;

        local entry_info = comm_center.call_service("Svr_GetAttachGameEntryInfo", 
                {game_id = tonumber(game_id), entry_show = 0, entry_title = "", entry_link = ""});
        
        if entry_info == nil or entry_info.entry_show == 0 or entry_info.entry_title == "" then
            ui_utility.setproperty_by_id(entry_id, "Visible", "false");
            return;
        end

        ui_utility.setproperty_by_id(entry_id, "Caption", entry_info.entry_title);
        ui_utility.setproperty_by_id(entry_id, "Visible", "true");
    end;

    -- 辅助设置
    refresh_assist_setting_btn = function(game_id)
        local panel_id = ui_utility.get_ctrl_id_by_path(helper_area.parent_id_, "HelperAreaCtn");
        if panel_id == nil or panel_id == Tenio.INVALID_CONTROLID then
            return;
        end

        MainCommon.SetButtonTextColor(ui_utility.uilib(), panel_id, "CtnMixBtns\\BtnAssistSetting", 
                                      wg_frame_bottom_view.res_cfg_.ASSIST_BTN_TXT_COLOR);

        local more_setting_id = ui_utility.get_ctrl_id_by_path(
                                    panel_id, "CtnMixBtns\\BtnAssistSetting");

        if helper_area.game_main_type_ == wg_frame_bottom_view.GAME_MAIN_TYPE_ENUM.CONSOLE_GAME then
            ui_utility.setproperty_by_id(more_setting_id, "Visible", "true");
            return;
        else
            ui_utility.setproperty_by_id(more_setting_id, "Visible", "false");
        end;

        local cur_game_id = game_id;
        local data = comm_center.call_service("Svr_GameHelperClosed",
                                              { game_id = cur_game_id });
        if data ~= nil and data.is_closed == 1 then 
            helper_area.inner_log("game_helper closed, hide plugin setting");
            return;
        end;
        -- 保存数据
        local plugin_setting_info_json = 
            frame_bottom_model.get_plugin_setting_info(cur_game_id);

        if "" == plugin_setting_info_json or nil == plugin_setting_info_json then
            helper_area.inner_log("plugin_setting_info_json empty");
            return;
        end
        
        local plugin_setting_info = base_utility.json_to_table(plugin_setting_info_json);

        if nil == plugin_setting_info or plugin_setting_info == "" then
            helper_area.inner_log("plugin_setting_info empty");
            return;
        end

        ui_utility.setproperty_by_id(more_setting_id, "Visible", "true");

        frame_bottom_model.plugin_more_setting_info[cur_game_id] = 
                                    plugin_setting_info.more_setting;
    end;

    on_attach_game_entry_click = function()
        if helper_area.game_main_type_ == wg_frame_bottom_view.GAME_MAIN_TYPE_ENUM.CONSOLE_GAME then
            plugin_tool_simple_panel.on_attach_game_entry_click();
        else
            plugin_tool_panel.on_attach_game_entry_click();
        end;
    end;

    on_assist_setting_btn_click = function()
        if helper_area.game_main_type_ == wg_frame_bottom_view.GAME_MAIN_TYPE_ENUM.CONSOLE_GAME then
            plugin_tool_simple_panel.on_cross_setting_btn_click();
        else
            plugin_tool_panel.on_assist_setting_btn_click();
        end;
    end;

    on_assist_btn_mouse_hover = function()
        local assist_btn_icon_id = ui_utility.get_ctrl_id_by_path(helper_area.parent_id_, 
                    "HelperAreaCtn\\CtnMixBtns\\BtnAssistSetting\\BtnSettingIcon");
        if assist_btn_icon_id == nil or assist_btn_icon_id == Tenio.INVALID_CONTROLID then
            return;
        end

        ui_utility.setproperty_by_id(assist_btn_icon_id, 
                            "Background", "ONEIMAGE:frame_bottom\\setting_hover.png;center;0;");
    end;

    on_assist_btn_mouse_leave = function()
        local assist_btn_icon_id = ui_utility.get_ctrl_id_by_path(helper_area.parent_id_, 
                    "HelperAreaCtn\\CtnMixBtns\\BtnAssistSetting\\BtnSettingIcon");
        if assist_btn_icon_id == nil or assist_btn_icon_id == Tenio.INVALID_CONTROLID then
            return;
        end

        ui_utility.setproperty_by_id(assist_btn_icon_id, 
                            "Background", "ONEIMAGE:frame_bottom\\setting_normal.png;center;0;");
    end;

    on_color_mode_update = function(data)
        if nil == data or nil == data.color_mode then
            return;
        end
        base_utility.log("[helper_area]on_color_mode_update: " .. tostring(data.color_mode), true);
        local color_mode = data.color_mode;
        -- TODO(planezhang): change tpf style
        local panel_id = ui_utility.get_ctrl_id_by_path(helper_area.parent_id_, "HelperAreaCtn");
        if panel_id == nil then
            return;
        end;
        local sText = "Common.xml/BottomBarButton/NormalBtnStyle";
        if color_mode == MainCommon.color_mode.kLight then
            sText = "Common.xml/BottomBarButton/NormalBtnLightStyle";
        end;
        local more_setting_id = ui_utility.get_ctrl_id_by_path(
                                    panel_id, "CtnMixBtns\\BtnAssistSetting");
        if more_setting_id ~= nil then
            ui_utility.setproperty_by_id(more_setting_id, "StylePath", sText);
        end;

        local btn_attach_game_entry = ui_utility.get_ctrl_id_by_path(
                                    panel_id, "CtnMixBtns\\BtnAttachGameEntry");
        if btn_attach_game_entry ~= nil then
            ui_utility.setproperty_by_id(btn_attach_game_entry, "StylePath", sText);
        end;

        local extension_btn_id = ui_utility.get_ctrl_id_by_path(panel_id, 
            "CtnMixBtns\\BtnDownloadExtension");
        ui_utility.setproperty_by_id(extension_btn_id, "StylePath", sText);
    end;

    on_download_extension_btn_click = function()
        local game_id = tonumber(helper_area.game_id_);
        local result = comm_center.call_service("Svr_GameLibrary_GetDownloadExtensionCfg",
                {game_id = game_id, need_show_btn = false, need_show_tips = false, cfg = ""});
        local need_show_tips = false;
        if result ~= nil then
            need_show_tips = result.need_show_tips;
        end

        if need_show_tips then
            local cfg = base_utility.json_to_table(result.cfg);
            base_utility.log(string.format("[frame_bottom]on_download_extension_btn_click, need show tips, tips_msg:%s, game_id:%u",
                cfg.tips_msg, game_id));
            comm_center.call_service("Svr_GameLibrary_MarkDownloadExtensionTipsShown",
                {game_id = game_id});
            local param = {
                position = "center",
                title = LoadStr("POP_WND_COMMON_TITLE"),
                width = 480,
                height = 230,
                titleIcon = "",
                icon = "",
                tipsIcon = poptips_icons.kInfo,
                descTitle = cfg.tips_msg,
                desc = "",
                okBtn = {
                    visible = "true",
                    text = LoadStr("POP_WND_COMMON_OK"),
                    handle = function()
                        comm_center.call_service("Svr_GameLibrary_DownloadExtensionPackage",
                                {game_id = game_id});
                        poptips_control.close_tips();
                    end
                },
                cancelBtn = {
                    visible = "true",
                    text = LoadStr("POP_WND_COMMON_CANCEL"),
                },
            };
            ui_utility.show_tips(param);
        else
            base_utility.log(string.format("[frame_bottom]on_download_extension_btn_click, don't show tips, game_id:%u",
                game_id));
            comm_center.call_service("Svr_GameLibrary_DownloadExtensionPackage",
                {game_id = game_id});
        end
    end;

    set_download_extension_btn_state = function()
        local game_id = tonumber(helper_area.game_id_);
        local panel_id = ui_utility.get_ctrl_id_by_path(helper_area.parent_id_, "HelperAreaCtn");
        if panel_id == nil or panel_id == Tenio.INVALID_CONTROLID then
            return;
        end

        local btn_id = ui_utility.get_ctrl_id_by_path(panel_id, "CtnMixBtns\\BtnDownloadExtension");
        local result = comm_center.call_service("Svr_GameLibrary_GetDownloadExtensionCfg",
                {game_id = game_id, need_show_btn = false, need_show_tips = false, cfg = ""});
        if result == nil or result.need_show_btn == false then
            ui_utility.setproperty_by_id(btn_id, "Visible", "false");
            return;
        end

        local game_state = banner_model.get_game_state(game_id);
        if game_state == nil then
            ui_utility.setproperty_by_id(btn_id, "Visible", "false");
            return;
        end;
        helper_area.cur_game_state_ = game_state.iState;

        if game_state.iState == ierd_tgp.game_library.GAME_STATE_UNINSTALLING or
           game_state.iState == ierd_tgp.game_library.GAME_STATE_DOWNLOADING or
           game_state.iState == ierd_tgp.game_library.GAME_STATE_DOWNLOAD_PAUSED or
           game_state.iState == ierd_tgp.game_library.GAME_STATE_DOWNLOAD_FAIL or
           game_state.iState == ierd_tgp.game_library.GAME_STATE_UPDATING or
           game_state.iState == ierd_tgp.game_library.GAME_STATE_UPDATE_PAUSED or
           game_state.iState == ierd_tgp.game_library.GAME_STATE_UPDATE_FAIL or
           game_state.iState == ierd_tgp.game_library.GAME_STATE_REPAIRING or
           game_state.iState == ierd_tgp.game_library.GAME_STATE_INSTALLING or
           game_state.iState == ierd_tgp.game_library.GAME_STATE_NEED_UPDATE then
            ui_utility.setproperty_by_id(btn_id, "Disable", "true");
        else
            ui_utility.setproperty_by_id(btn_id, "Disable", "false");
        end

        local cfg = base_utility.json_to_table(result.cfg);
        ui_utility.setproperty_by_id(btn_id, "Caption", cfg.download_extension_btn_text);
        ui_utility.setproperty_by_id(btn_id, "Visible", "true");
        base_utility.log(string.format("[frame_bottom]set_download_extension_btn_state, btn_text:%s, game_id:%u",
                cfg.download_extension_btn_text, game_id));
    end;

    on_game_state_update = function(state)
        if state ~= nil and helper_area.cur_game_state_ ~= state then
            helper_area.cur_game_state_ = state;
            helper_area.set_download_extension_btn_state();
        end
    end;
};

helper_area.init();